﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.Communication;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public class ClientCommu : IClientCommunication, IDisposable
    {
        IServerCommunication serverCommunication = null;
        ForumService forumService = null;
        int forum;


        public ClientCommu(ForumService fs, int forum)
        {
            this.forumService = fs;
            InitCommunication();
            this.forum = forum;
            //probably need to add a call to enter a certain forum here
        }

        public void InitCommunication()
        {
            DuplexChannelFactory<IServerCommunication> communicationFactory =
                  new DuplexChannelFactory<IServerCommunication>(
                      new InstanceContext(this),
                      new NetTcpBinding(),
                      new EndpointAddress("net.tcp://localhost:8000/ISubscribe"));
            try
            {
                serverCommunication = communicationFactory.CreateChannel();
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Couldn't establish a connection");
            }
        }

        //callbacks

        public void ReceiveJoin(Common.DataBaseNew.User client, bool ans, string systemMsg)
        {
            if (!ans)
            {
                new FormError("Illegal Username OR Password!").Show();
            }
            else
            {
                this.forumService.receiveJoin(client);
            }
        }

        public void ReceiveLogout()
        {
        }

        public void ReceiveNotify(string notifyMsg)
        {
            this.forumService.receiveUpdateList(notifyMsg);
        }

        public void ReceiveUsersList(List<string> uList)
        {
            this.forumService.receiveUsersList(uList);
        }

        public void AutherizationError()
        {
            this.forumService.autherizationError();
        }

        public void ReceiveForum(Common.DataBaseNew.Forum forum)
        {
            this.forumService.receiveForum(forum);
        }

        public void ReceiveSubForum(Common.DataBaseNew.SubForum subforum)
        {
            this.forumService.receiveSubForum(subforum);
        }

        public void ReceiveThread(Common.DataBaseNew.Thread thread)
        {
            this.forumService.receiveThread(thread);
        }

        public void ReceiveSystemStatistics(List<string> uList, string stats, Common.DataBaseNew.User me)
        {
            this.forumService.receiveAdminAccess(uList, stats, me);
        }

        public void ReceiveForumStatistics(string stats)
        {
            this.forumService.ReceiveForumStatistics(stats);
        }


        //server functions


        internal void logout(Common.DataBaseNew.User me)
        {
            this.serverCommunication.Logout(me, this.forum);
        }


        internal void getUserList(Common.DataBaseNew.User me)
        {
            this.serverCommunication.getForumUserList(me, forum);
        }

        internal void getMainForum(Common.DataBaseNew.User me)
        {
            this.serverCommunication.GetForum(new ClientRequest(me,this.forum));
        }

        internal void getSubForum(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.GetSubForum(clientRequest);
        }

        internal void getThread(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.GetThread(clientRequest);
        }

        internal void promote(Common.DataBaseNew.User me, string name, string forum)
        {
            this.serverCommunication.AddModerator(me, name, forum, this.forum);
        }

        internal void AddForum(Common.DataBaseNew.User me, string name, string forum)
        {
            this.serverCommunication.AddForum(me, name, forum);
        }

        internal void register(string name, string last, string nick, string pass, string email)
        {
            this.serverCommunication.Register(name, last, nick, pass, email, this.forum);
        }

        internal void login(string flag, string user, Byte[] password)
        {
            this.serverCommunication.Login(flag, user, password, this.forum);
        }

        internal void postReply(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.AddPost(clientRequest);
        }

        internal void newThread(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.AddThread(clientRequest);
        }

        internal void newSub(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.AddSubForum(clientRequest);
        }

        internal void exchangeModerator(Common.DataBaseNew.User me, string oldMod, string newMod, string forum)
        {
            this.serverCommunication.ExchangeModerator(me,oldMod, newMod, forum, this.forum);
        }


        internal void deleteThread(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.DeleteThread(clientRequest);
        }


        internal void deletePost(ClientRequest clientRequest)
        {
            this.serverCommunication.DeletePost(clientRequest);
        }

        internal void demote(Common.DataBaseNew.User me, string name, string forum)
        {
            this.serverCommunication.DeleteModerator(me, name, forum, this.forum);
        }

        internal void deleteSubForum(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.DeleteSubForum(clientRequest);
        }

        internal void getForumStats(Common.DataBaseNew.User me)
        {
            this.serverCommunication.GetForumStatistics(me, forum);
        }

        internal void getForumsStats(string user, string password)
        {
            this.serverCommunication.GetSystemStatistics(user, password);
        }

        internal void editThread(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.EditThread(clientRequest);
        }

        internal void editPost(Common.ClientRequest clientRequest)
        {
            this.serverCommunication.EditPost(clientRequest);
        }


        public void Dispose()
        {
            this.Dispose();
        }


        public void ReceiveForumUsersList(List<string> uList)
        {
            this.forumService.receiveUsersList(uList);
        }

        public void ReceiveSystemUsersList(List<string> uList)
        {
            throw new NotImplementedException();
        }

        public void RecieveError(string content)
        {
            this.forumService.RecieveError(content);
        }




        internal void adminExit(Common.DataBaseNew.User me)
        {
            this.serverCommunication.AdminExit(me);
        }
    }
}
